package constant.milk.periodapp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import constant.milk.periodapp.alarm.MilkAlarm;
import constant.milk.periodapp.bean.SengriData;
import constant.milk.periodapp.database.DBSelectQuery;
import constant.milk.periodapp.database.DbAdapter;
import constant.milk.periodapp.database.DbInsertQuery;
import constant.milk.periodapp.util.MilkShared;
import constant.milk.periodapp.util.MilkUtil;
import android.content.Context;

public class SengriCheck
{
	private Context context;
	private DbAdapter db;
	
	private DBSelectQuery select;
	private DbInsertQuery insert;
	private MilkShared s;
	
	public SengriCheck(Context context, DbAdapter db)
	{
		this.context = context;
		this.db = db;
	}
	
	public void setData()
	{
		select = new DBSelectQuery(context, db);
		insert = new DbInsertQuery(context, db);
		s = new MilkShared(context);
		
		ArrayList<SengriData> list = new ArrayList<SengriData>();
		list = select.getSengriList();
		
		if (list.size() > 0)
		{
			SengriData data = select.getSengriMaxDate();
			
			Calendar cal1 = Calendar.getInstance();
			
			Calendar cal2 = Calendar.getInstance();
			cal2.set(data.getYear(), data.getMonth(), data.getDay());
			cal2.add(Calendar.MONTH, 1);
			
			long mill1 = cal1.getTimeInMillis();
			long mill2 = cal2.getTimeInMillis();
			
			while (mill1 > mill2)
			{
				setSengriData(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH));
				
				cal2.add(Calendar.MONTH, 1);
				
				mill2 = cal2.getTimeInMillis();
			}
		}
	}
	
	private int setSengriData(int year, int month)
	{
		ArrayList<SengriData> list = new ArrayList<SengriData>();
		list = select.getSengriList();
		
		if (list.size() < 1)
		{
			return 0;
		}
		
		int term = s.getSengriTerm();
		
		if (s.getSengriType() == 0)
		{
			term = s.getSengriTerm();
		}
		else
		{
			term = MilkUtil.getAveSengriTerm(context, select);
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, 1);
		
		for (int i = 1; i < calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1; i++) 
		{
			int sengriDate = s.getSengriDate();
			
			SengriData data = select.getSengriMaxDate();
			Calendar cal2 = Calendar.getInstance();
			cal2.setTimeInMillis(0);
			cal2.set(data.getYear(), data.getMonth(), data.getDay());
			
			long mill2 = cal2.getTimeInMillis();
			
			for (int j = 0; j < sengriDate; j++)
			{
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(0);
				cal.set(year, month, i);
				cal.add(Calendar.DAY_OF_MONTH, -j);
				long mill1 = cal.getTimeInMillis();
				
				mill1 = mill1 - mill2;
				
				long mog = mill1 / (24 * 60 * 60 * 1000);
				
				long sengriTerm = mog / term;
				long namugi = mog % term;
				
				if (sengriTerm > 0 && namugi == 0)
				{
					Calendar cal3 = Calendar.getInstance();
					cal3.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
					cal3.add(Calendar.DAY_OF_MONTH, - (int) mog);
					
					list = select.getSengriDate(cal3.get(Calendar.YEAR), cal3.get(Calendar.MONTH), cal3.get(Calendar.DAY_OF_MONTH));
					
					if (list.size() > 0)
					{
						if (j == 0)
						{
							if (setPeriodData(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)))
							{
								MilkAlarm ma = new MilkAlarm();
								ma.checkAlarm(context, db);
							}
							
							return 2;
						}
						else
						{
							return 1;
						}
					}
				}
			}
		}
		
		return 0;
	}
	
	private boolean setPeriodData(int year, int month, int day)
	{
		try
		{
			insert.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			MilkShared s = new MilkShared(context);
			
			map.put("year", year + "");
			map.put("month", month + "");
			map.put("day", day + "");
			map.put("term", s.getSengriDate() + "");
			map.put("date", MilkUtil.getDateDB(year, month, day));
			
			insert.setPeriodDate(map);
			
			insert.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				insert.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
}
